package dao

import javax.inject.Inject
import models.Tables._
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.jdbc.JdbcProfile

import scala.concurrent.{ExecutionContext, Future}

class GenomeDao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider)
                         (implicit exec: ExecutionContext) extends
  HasDatabaseConfigProvider[JdbcProfile] {

  import profile.api._

  def insert(row: Seq[GenomeRow]): Future[Unit] = {
    db.run(Genome ++= row).map(_ => ())
  }

  def getAllGenome: Future[Seq[GenomeRow]] = {
    db.run(Genome.result)
  }

  def getByIds(ids:Seq[Int]) : Future[Seq[GenomeRow]] ={
    db.run(Genome.filter(_.id.inSetBind(ids)).result)
  }

  def getByGeneIds(geneids: Seq[String], species: String): Future[Seq[GenomeRow]] = {
    db.run(Genome.filter(_.species === species).filter(_.geneid.inSetBind(geneids)).result)
  }

  def getByGeneIds(geneids: Seq[String]): Future[Seq[GenomeRow]] = {
    db.run(Genome.filter(_.geneid.inSetBind(geneids)).result)
  }


  def getByRange(species: String, chr: String, start: Int, end: Int): Future[Seq[GenomeRow]] = {
    db.run(Genome.filter(_.species === species).filter(_.chr === chr).filter(_.start >= start).filter(_.end <= end).result)
  }

  def getById(id: Int): Future[GenomeRow] = {
    db.run(Genome.filter(_.id === id).result.head)
  }

  def getByGeneId(geneid: String): Future[GenomeRow] = {
    db.run(Genome.filter(_.geneid === geneid).result.head)
  }

  def updateGoAndKegg(id:Int,go:String,ko:String): Future[Unit] = {
    db.run(Genome.filter(_.id === id ).map(x=> (x.gos,x.keggKo)).update(go,ko)).map(_=>())
  }

}
